# -*- coding:utf-8 -*-

import os
import django
from celery.schedules import crontab
from django.conf import settings
from celery import Celery

# 创建celery实例对象
app = Celery('celery_Demo')

# 把celery和django进行组合，识别和加载django的配置文件
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Main.settings')
django.setup()
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# 加载任务
# 参数必须必须是一个列表，里面的每一个任务都是任务的路径名称
# app.autodiscover_tasks(["任务1","任务2"])

# 或者下面这种
# Load task modules from all registered Django apps.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# 时区
app.conf.timezone = 'Asia/Shanghai'
# 是否使用UTC
app.conf.enable_utc = False

app.conf.beat_scheduler = 'django_celery_beat.schedulers:DatabaseScheduler'

app.conf.beat_schedule = {
    'add-every-monday-morning': {
        'task': 'apps.school.tasks.add',
        'schedule': crontab(minute='*/1'),
        'args': (16, 16),
    },
}
